/**
 * 
 */
package net.mikoo.seals.boss.common.helper;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import net.mikoo.seals.boss.common.enums.MobileStatusEnum;
import net.mikoo.seals.boss.common.enums.PartyStatusEnum;
import net.mikoo.seals.boss.common.utils.DateUtil;
import net.mikoo.seals.boss.model.Party;
import net.mikoo.seals.boss.model.Person;
import net.mikoo.seals.boss.model.UserLogin;

/**
 * @author huangxm
 *
 */
public class FormHelper {
	
	private Logger loggger = Logger.getLogger(FormHelper.class);
	
	public Long getLong(HttpServletRequest request,String parm){
		
		String data = request.getParameter(parm);
		try {
			return Long.parseLong(data);
		} catch (Exception e) {
			loggger.debug(e);
		}
		return null;
	}
	
	public String getString(HttpServletRequest request,String parm){
		
		String data = request.getParameter(parm);
		return data;
	}
	
	public Date getDate(HttpServletRequest request,String parm){
		String data = request.getParameter(parm);
		try {
			
			return DateUtil.stringToDate(data, "yyyy-MM-dd HH:mm:ss");
		} catch (Exception e) {
			loggger.debug(e);
		}
		return null;
	}
	
	public Double getDouble(HttpServletRequest request,String parm){
		
		String data = request.getParameter(parm);
		try {
			return Double.parseDouble(data);
		} catch (Exception e) {
			loggger.debug(e);
		}
		return null;
	}
	
	public Float getFloat(HttpServletRequest request,String parm){
		
		String data = request.getParameter(parm);
		try {
			return Float.parseFloat(data);
		} catch (Exception e) {
			loggger.debug(e);
		}
		return null;
	}
	
	
	
	/**
	 * 获取操作员操作表单
	 * @param request
	 * @return
	 */
	public Party getCreatOperator(HttpServletRequest request){
		
		String userLoginId = request.getParameter("username");
		String currentPassword = request.getParameter("password");
		String expiryDate = request.getParameter("expiryDate");
		String partyStatus = request.getParameter("partyStatus");
		String name = request.getParameter("name");
		String gender = request.getParameter("gender");
		String nickname = request.getParameter("nickname");
		String email = request.getParameter("email");
		String mobile = request.getParameter("mobile");
		String telephone = request.getParameter("telephone");
		String qq = request.getParameter("qq");
		String weixinId = request.getParameter("weixinId");
		String birthday = request.getParameter("birthday");
		String partyId = request.getParameter("partyId");
		
		String companyId = request.getParameter("companyId");
		String idcard = request.getParameter("idcard");
		String idcardFront = request.getParameter("idcardFront");
		String idcardBack = request.getParameter("idcardBack");
		
		
		String roleId = request.getParameter("roleId");
		
		Party party = new Party();
		party.setPartyId(partyId == null ? null : Long.valueOf(partyId));
		party.setUserLoginId(userLoginId);
		party.setExpiryDate(DateUtil.stringToDate(expiryDate, "yyyy-MM-dd"));
		party.setPartyStatus(StringUtils.isNotBlank(partyStatus)?partyStatus:PartyStatusEnum.NORMAL.code());
		party.setRoleId(roleId == null ? null : Long.valueOf(roleId));
		party.setCompanyId(companyId == null ? null : Long.valueOf(companyId));
		
		UserLogin userLogin = new UserLogin();
		userLogin.setPartyId(partyId == null ? null : Long.valueOf(partyId));
		userLogin.setUserLoginId(userLoginId);
		userLogin.setCurrentPassword(currentPassword);
		userLogin.setRequirePasswordChange("N");
		
		Person person = new Person();
		person.setPartyId(partyId == null ? null : Long.valueOf(partyId));
		person.setUserLoginId(userLoginId);
		person.setName(name);
		person.setGender(gender);
		person.setNickname(nickname);
		person.setEmail(email);
		person.setMobileStatus(MobileStatusEnum.NOT_CERTIFIED.code());
		person.setMobile(StringUtils.isBlank(mobile)?userLoginId:mobile);
		person.setTelephone(telephone);
		person.setQq(qq);
		person.setWeixinId(weixinId);
		person.setBirthday(DateUtil.stringToDate(birthday, "yyyy-MM-dd"));
		
		person.setIdcard(idcard);
		person.setIdcardFront(idcardFront);
		person.setIdcardBack(idcardBack);
		
		
		party.setUserLogin(userLogin);
		party.setPerson(person);
		return party;
	}
	
	public Party getModifyOperator(HttpServletRequest request){
		String partyId = request.getParameter("partyId");
		Party party =  getCreatOperator(request);
		party.setPartyId(Long.parseLong(partyId));
		return party;
	}
	

	
	
	

}
